﻿using System.Threading;
using System.Windows;
using System.Windows.Threading;

namespace WholeSlideImageViewer
{
    /// <summary>
    /// ExportProgressDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ExportProgressDialog : Window
    {
        private readonly ExportProgressDialogViewModel? _viewModel;

        public ExportProgressDialog()
        {
            InitializeComponent();
        }

        internal ExportProgressDialog(IExportProgress progress, CancellationTokenSource cts)
        {
            InitializeComponent();

            var viewModel = new ExportProgressDialogViewModel(progress, cts);
            DataContext = viewModel;

            viewModel.RequestClose += OnRequestClose;

            _viewModel = viewModel;
        }

        private void OnRequestClose()
        {
            Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()
            {
                Close();
            });
        }

        private void Window_Closed(object sender, System.EventArgs e)
        {
            _viewModel?.Cancel();
        }
    }
}
